import java.util.Arrays;

/**
 * 561. 数组拆分 I
 * https://leetcode-cn.com/problems/array-partition-i/
 */
public class Solutions_561 {
    public static void main(String[] args) {
        int[] nums = {1, 4, 3, 2};  // output: 4
//        int[] nums = {6, 2, 6, 5, 1, 2};  // output: 9

        int result = arrayPairSum(nums);
        System.out.println(result);
    }

    /**
     * 思路：{1, 2, 3, 4}，将 {1, 2}，{3, 4} 成队，所得的结果最大
     */
    public static int arrayPairSum(int[] nums) {
        if (nums == null) {
            return 0;
        }
        // 排序
        Arrays.sort(nums);
        int res = 0;
        // 偶数索引位上的元素
        for (int i = 0; i < nums.length; i += 2) {
            res += nums[i];
        }
        return res;
    }
}
